<template>
  <div class='article-add'>
    <!-- 增加文章的对话框 -->
    <el-dialog
       title="新增文章"
       :visible.sync="dialogVisible"
       width="60%"
      @close='$refs.addFormRef.resetFields()'
    >
      <!-- 表单区域 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px" class="demo-ruleForm" >
        <!-- 文章标题 -->
        <el-form-item label="文章标题 :" prop="title">
          <el-input v-model="addForm.title" placeholder="请输入文章标签"></el-input>
        </el-form-item>
        <!-- 文章内容 -->
        <el-form-item label="文章内容 :" prop="articleBody">
          <quill-editor v-model="addForm.articleBody" :options="editorOption"></quill-editor>
        </el-form-item>
        <!-- 视频地址 -->
        <el-form-item label="视频地址 :" >
          <el-input v-model="addForm.videoURL" placeholder="请输入视频地址"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <!-- 取消按钮 -->
        <el-button @click="dialogVisible = false">取 消</el-button>
        <!-- 确定按钮 -->
        <el-button type="primary" @click="addArticle">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改文章的对话框 -->
    <el-dialog
      title="修改文章"
      :visible.sync="editDialogVisible"
      width="50%"
      @close='$refs.editFormRef.resetFields()'
    >
      <!-- 表单区域 -->
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px" >
        <!-- 文章标题 -->
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="editForm.title"></el-input>
        </el-form-item>
        <!-- 文章内容 -->
        <el-form-item label="文章内容" prop="articleBody">
          <quill-editor v-model="editForm.articleBody" :options="editorOption"></quill-editor>
        </el-form-item>
        <!-- 视频地址 -->
        <el-form-item label="视频地址" >
          <el-input v-model="editForm.videoURL"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer btn-search">
        <!-- 取消按钮 -->
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <!-- 确定按钮 -->
        <el-button type="primary" @click="editArticle">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 文章添加 文章修改
import { add, update } from '@/api/hmmm/articles'
// 导入富文本的包
import { quillEditor } from 'vue-quill-editor'
// 导入富文本的css样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

export default {
  data () {
    // 视频格式的表单验证
    const checkVideoURL = (rule, value, callback) => {
      const regvideoURL = /^https?:\/\/(.+\/)+.+(\.(swf|avi|flv|mpg|rm|mov|wav|asf|3gp|mkv|rmvb|mp4))$/i
      if (!regvideoURL.test(value)) return callback(new Error('您的视频地址格式不正确，请重新输入'))
      callback()
    }
    return {
      dialogVisible: false,
      // 文章添加
      addForm: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      // 表单验证
      addFormRules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' }

        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'change' }
        ],
        videoURL: [
          { validator: checkVideoURL, trigger: 'blur' }
        ]
      },
      editDialogVisible: false,
      // 文章修改
      editForm: {},
      // 表单修改验证
      editFormRules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' }

        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'change' }
        ],
        videoURL: [
          { validator: checkVideoURL, trigger: 'blur' }
        ]
      },
      // 富文本
      editorOption: {
        placeholder: '',
        modules: {
          toolbar: [
            // 加粗 斜体 下划线 删除线
            ['bold', 'italic', 'underline', 'strike'],
            // 有序列表 无序列表
            [{ list: 'ordered' }, { list: 'bullet' }],
            // 引用 代码块 图片 链接
            ['blockquote', 'code-block', 'image', 'link']
          ]
        }
      }
    }
  },
  methods: {
    // 添加文章
    addArticle () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        const regvideoURL = /^https?:\/\/(.+\/)+.+(\.(swf|avi|flv|mpg|rm|mov|wav|asf|3gp|mkv|rmvb|mp4))$/i
        if (!regvideoURL.test(this.addForm.videoURL)) this.addForm.videoURL = ''
        const res = await add(this.addForm)
        // console.log(res)
        if (res.status !== 200) return this.$message.error('添加文章失败')

        this.$message.success('添加文章成功')
        this.$parent.getArticles()
        this.dialogVisible = false
      })
    },
    // 修改文章
    editArticle () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        const regvideoURL = /^https?:\/\/(.+\/)+.+(\.(swf|avi|flv|mpg|rm|mov|wav|asf|3gp|mkv|rmvb|mp4))$/i
        const res = await update({
          id: this.editForm.id,
          title: this.editForm.title,
          articleBody: this.editForm.articleBody,
          videoURL: regvideoURL.test(this.editForm.videoURL) ? this.editForm.videoURL : ''
        })
        console.log(res)
        this.$parent.getArticles()
        this.editDialogVisible = false
      })
    }
  },
  components: {
    quillEditor
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .ql-editor{
  height: 200px;
}
::v-deep .ql-toolbar.ql-snow{
  padding: 0 8px;
}
.btn-search {
  display: flex;
  flex: 1;
  justify-content: flex-end;
}
</style>
